package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.LeaveManagement;
import com.example.prison.entity.vo.PeopleDestination;
import com.example.prison.service.LeaveManagementService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@Api(tags = "请假管理")
@RestController
@RequestMapping("/api")
public class LeaveManagementController {

    @Autowired
    private LeaveManagementService leaveManagementService;

//    @Autowired
//    private SystemLogService systemLogService;

    @ApiOperation("获取请假列表")
    @GetMapping("/getLeaves")
    public Response<PageInfo<LeaveManagement>> getLeaves(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                         @Parameter(description = "请假去向")@RequestParam String destination,
                                                         @Parameter(description = "状态(未审核/同意/不同意/未销假/已销假)")@RequestParam(value = "status") String status,
                                                         @RequestParam(value = "beginTime", defaultValue = "0000-00-00 00:00") String beginTime,
                                                         @RequestParam(value = "endTime", defaultValue = "9999-12-31 23:59") String endTime) {
        PageInfo<LeaveManagement> pageInfo = leaveManagementService.getLeaves(pageNumber, pageSize, destination, status, beginTime, endTime);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("提交请假申请")
    @PostMapping("/addLeave")
    public Response<?> addLeave(@RequestBody LeaveManagement leave) {
        if(leave.getBeginDate().compareTo(leave.getEndDate()) > 0) {
            return Response.error("时间提交错误！请检查时间！");
        }
        int result = leaveManagementService.addLeave(leave);
        if (result == -1) {
            return Response.error("该时间段已提交请假申请！");
        } else if (result == 0) {
            return Response.error("无此权限！");
        }
        log.info("提交请假申请"+", 数据："+ JSON.toJSONString(leave));
//        systemLogService.addLog("提交请假申请");
        return Response.success("提交成功！");
    }

    @ApiOperation("删除请假条(仅未审核或已销假)")
    @PostMapping("/deleteLeave")
    public Response<?> deleteLeave(@RequestBody Integer[] ids) {
        String str = "";
        for (Integer id : ids) {
            LeaveManagement leave = leaveManagementService.getLeavesById(id);
            if(leave != null && (Objects.equals(leave.getStatus(), "未审核") || Objects.equals(leave.getStatus(), "已销假") || Objects.equals(leave.getStatus(), "不同意"))) {
                leaveManagementService.deleteLeaveById(id);
            } else if(leave == null) {
                return Response.info("请假申请不存在");
            } else {
                str = str + "," + leave.getId();
            }
        }
        if(ObjectUtils.isEmpty(str)) {
            log.info("删除请假条");
//            systemLogService.addLog("删除请假条");
            return Response.success("删除成功！");
        } else {
            return Response.info("请假申请未销假，不可删除");
        }
    }
    @ApiOperation("删除请假条(全部可删)")
    @PostMapping("/deleteAllLeave")
    public Response<?> deleteAllLeave(@RequestBody Integer[] ids) {
        leaveManagementService.deleteLeave(ids);
        log.info("删除请假条");
//        systemLogService.addLog("删除请假条");
        return Response.success("删除成功！");
    }

    @ApiOperation("审批请假")
    @PostMapping("/checkLeave")
    public Response<?> checkLeave(@RequestBody LeaveManagement leave) {
        Integer result = leaveManagementService.checkLeave(leave);
        if(result == 1) {
            log.info("审批请假"+", 数据："+JSON.toJSONString(leave));
//            systemLogService.addLog("审批请假");
            return Response.success("审批成功！");
        } else {
            return Response.error("该请假条已审核");
        }
    }

    @ApiOperation("依据id获取请假条")
    @GetMapping("/getLeavesById")
    public Response<LeaveManagement> getLeavesById(@RequestParam(value = "id") Integer id) {
        LeaveManagement leaves = leaveManagementService.getLeavesById(id);
        if(ObjectUtils.isEmpty(leaves)) {
            return Response.error("此id不存在！");
        }
        return Response.success("获取成功！", leaves);
    }

//    @ApiOperation("依据状态获取请假列表")
//    @GetMapping("/queryLeaves")
//    public Response<PageInfo<LeaveManagement>> queryLeaves(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                           @Parameter(description = "状态(未审核/同意/不同意/未销假/已销假)")@RequestParam(value = "status") String status) {
//        PageInfo<LeaveManagement> pageInfo = leaveManagementService.getLeavesByStatus(pageNumber, pageSize, status);
//        return Response.success("获取成功！", pageInfo);
//    }
//
//    @ApiOperation("依据时间获取请假列表")
//    @GetMapping("/getLeavesByTime")
//    public Response<PageInfo<LeaveManagement>> getLeavesByTime(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                     @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                     @RequestParam(value = "beginDate", defaultValue = "0000-00-00 00:00") String beginDate,
//                                                     @RequestParam(value = "endDate", defaultValue = "9999-12-31 23:59") String endDate) {
//        PageInfo<LeaveManagement> pageInfo = leaveManagementService.getLeavesByTime(pageNumber, pageSize, beginDate, endDate);
//        return Response.success("获取成功！", pageInfo);
//    }

    @ApiOperation("修改请假信息(仅未审核)")
    @PostMapping("/updateLeave")
    public  Response<?> updateLeave(@RequestBody LeaveManagement leave) {
        Integer result = leaveManagementService.updateLeave(leave);
        if(result == 1) {
            log.info("修改请假信息"+", 数据："+JSON.toJSONString(leave));
//            systemLogService.addLog("修改请假信息");
            return Response.success("修改成功！");
        } else {
            return Response.error("该请假条已审核");
        }
    }

    @ApiOperation("修改状态为未销假")
    @PostMapping("/updateStatus")
    public  Response<?> updateStatus(@RequestBody LeaveManagement leave) {
        Integer result = leaveManagementService.updateStatus(leave);
        if(result == 1) {
            return Response.success("修改成功！");
        } else {
            return Response.error("该请假条状态未通过");
        }
    }

    @ApiOperation("提前销假")
    @PostMapping("/earlyEndLeave")
    public  Response<?> earlyEndLeave(@RequestBody LeaveManagement leaveManagement) {
        Integer result = leaveManagementService.earlyEndLeave(leaveManagement);
        if(result == 1) {
            return Response.success("提前销假成功！");
        } else {
            return Response.error("该请假条状态不是未销假");
        }
    }

    @ApiOperation("销假")
    @PostMapping("/endLeave")
    public  Response<?> endLeave(@RequestBody LeaveManagement leaveManagement) {
        Integer result = leaveManagementService.endLeave(leaveManagement);
        if(result == 1) {
            return Response.success("销假成功！");
        } else {
            return Response.error("该请假条状态不是未销假");
        }
    }

//    @ApiOperation("获取到期未归请假人员")
//    @GetMapping("/getNoReturnLeavePeople")
//    public Response<PageInfo<LeaveManagement>> getNoReturnLeavePeople(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
//        PageInfo<LeaveManagement> pageInfo = leaveManagementService.getNoReturn(pageNumber, pageSize);
//        return Response.success("获取成功！", pageInfo);
//    }
//
//    @ApiOperation("获取长期未归预警请假人员")
//    @GetMapping("/getLongTimeLeavePeople")
//    public Response<PageInfo<LeaveManagement>> getLongTimeLeavePeople(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
//        PageInfo<LeaveManagement> pageInfo = leaveManagementService.getLongTime(pageNumber, pageSize);
//        return Response.success("获取成功！", pageInfo);
//    }
//
//    @ApiOperation("依据请假去向获取请假列表")
//    @GetMapping("/getLeaveByDestination")
//    public Response<PageInfo<LeaveManagement>> getLeaveByDestination(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                           @Parameter(description = "请假去向")@RequestParam String destination) {
//        PageInfo<LeaveManagement> pageInfo = leaveManagementService.getLeaveByDestination(pageNumber, pageSize, destination);
//        return Response.success("获取成功！", pageInfo);
//    }

    @ApiOperation("获取请假去向及其人员和数量")
    @GetMapping("/getLeaveDestinationAndPeople")
    public Response<List<PeopleDestination>> getLeaveDestinationAndPeople() {
        List<PeopleDestination> list = leaveManagementService.getLeaveDestinationAndPeople();
        return Response.success("获取成功！",list);
    }


}
